package com.nowcoder.community.service;

import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

/**
 * @author AnYang
 * @Description 点赞业务层，因为mapper层简单，所以直接写业务层
 * @Version 1.0.0
 * @Date 2024/1/28
 */

@Service
public class LikeService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Author AnYang
     * @Description 点赞
     * @Date 2024/1/28
     * @param: userId      点赞人id
     * @param: entityType  评论类型
     * @param: entityId    评论id
     * @return
     */
    public void like(int userId, int entityType, int entityId, int entityUserId) {
        /*String likeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);

        // 判断该用户是否点赞
        Boolean isMember = redisTemplate.opsForSet().isMember(likeKey, userId);
        if (isMember) {
            // 如果已经点赞，继续点赞就视为取消点赞，移除该用户id
            redisTemplate.opsForSet().remove(likeKey, userId);
        } else {
            // 没有点赞，加进来
            redisTemplate.opsForSet().add(likeKey, userId);
        }*/

        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
                String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);

                boolean isMember = operations.opsForSet().isMember(entityLikeKey, userId);

                operations.multi();

                if (isMember) {
                    operations.opsForSet().remove(entityLikeKey, userId);
                    operations.opsForValue().decrement(userLikeKey);
                } else {
                    operations.opsForSet().add(entityLikeKey, userId);
                    operations.opsForValue().increment(userLikeKey);
                }
                return operations.exec();
            }
        });
    }

    // 获取点赞数量
    public long findEntityLikeCount(int entityType, int entityId) {
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        return redisTemplate.opsForSet().size(entityLikeKey);
    }

    /**
     * @Author AnYang
     * @Description 显示是否点赞
     * @Date 2024/1/28
     * @param: userId
     * @param: entityType
     * @param: entityId
     * @return int    将来做业务扩展时，可能会有踩、赞等，所以用int
     */
    public int findEntityLikeStatus(int userId, int entityType, int entityId) {
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        return redisTemplate.opsForSet().isMember(entityLikeKey, userId) ? 1 : 0;
    }

    // 查询获得的赞
    public int findUserLikeCount(int userId) {
        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
        return count == null ? 0 : count.intValue();
    }
}
